<?php
namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use frontend\models\User;
use frontend\models\hobby\Hobby;
use frontend\models\hobby\HobbyRole;
use frontend\models\code\Code;
use frontend\models\Codes;
use frontend\models\Codee;
use frontend\models\region\Region;
use backend\models\course\Course;
use backend\models\friend\Friend;
use frontend\models\UserCourse;
use frontend\models\Wallet;
use frontend\models\UserMessage;
use yii\data\Pagination;
use backend\models\article\Article;
use frontend\models\Collection;
use frontend\models\teacher\Teacher;
use frontend\models\teacher\Like;
use frontend\models\comment\Comment;
use common\components\Easemob;
use frontend\models\question\Answer;
use frontend\models\question\Question;
use frontend\models\question\Queslabel;
use frontend\models\question\Questionlabel;
/*
*	模块：个人中心
*	作者：徐焕庭
*	时间：2015-11-26
*/

class CenterController extends Controller{
    /**
     * 方法执行前
     */
    public function beforeAction($action){
        parent::beforeAction($action);
        $session = Yii::$app->session;
		$uid = $session->get('user_id');
        
        if (empty($uid)) { 
            $this->goHome();
        } else {
            return true;    
        };
    }
	
	public function actions()
	{
		return [
			 'captcha' => [
				  'class' => 'yii\captcha\CaptchaAction',
				  'maxLength' => 5,
				  'minLength' => 5
			 ],
		 ];
	 }

	public $enableCsrfValidation = false; 
	//个人中心首页展示
	public function actionIndex(){
		$session = Yii::$app->session;
		$uid = $session->get('user_id');	//取出当前登录用户的用户id	
		$model = User::find()->where(["user_id"=>$uid])->one();
		$region = new Region();
		//省
		$province = $region->getProvince();
		if(empty($province)){
			$provinces['']='请选择';
		}else{
			foreach($province as $k=>$v){
				$provinces[$v['region_id']] = $v['region_name'];
			}
		}
		//城市
		$city = $region->getCity($model->province_id);
		if(empty($city)){
			$citys[''] = '请选择';
		}else{
			foreach($city as $k=>$v){
				$citys[$v['region_id']] = $v['region_name'];
			}
		}
		

		//县
		$area = $region->getArea($model->city_id);
		if(empty($area)){
			$areas[''] = '请选择';
		}else{
			foreach($area as $k=>$v){
				$areas[$v['region_id']] = $v['region_name'];
			}
		}
		$hobbyrole = HobbyRole::find()->where(['user_id'=>$uid])->asarray()->all();
		if(!empty($hobbyrole)){
			for($i=0;$i<count($hobbyrole);$i++){
				$re = Hobby::find()->where(['hobby_id'=>$hobbyrole[$i]['hobby_id']])->asarray()->one();
				$hobbyrole[$i]['hobby_name'] = $re['hobby_name'];
				$data[] = $hobbyrole[$i]['hobby_id'];
			}
			$db1 = Hobby::find()->asarray()->all();
			$db2 = Hobby::find()->where(['in','hobby_id',$data])->asarray()->all();
			foreach($db1 as $key => $valueA){
				$id = $valueA['hobby_id'];
					foreach($db2 as $keys => $valueB){
					if($id == $valueB['hobby_id']){
						unset($db1[$key]);
					}
				}
			}
			$db = $db1;
		}else{
			$db = Hobby::find()->asarray()->all();
		}
		$code = new Codes();
		$codee = new Codee();
		//$email = new Codes();
		//$email->setScenario('email');
		return $this->render('index',array( 

			'info'=>$info,
			'model'=>$model,
			'db'=>$db,
			'hobbyrole'=>$hobbyrole,
			'provinces'=>$provinces,
			'city'=>$citys,
			'area'=>$areas,
			'code'=>$code,
			'codee'=>$codee,
			));
	}

	//上传头像
	public function actionAvatar(){
		$session = Yii::$app->session;
		$uid = $session->get('user_id');	//取出当前登录用户的用户id	
		$model = User::find()->where(["user_id"=>$uid])->one();
		return $this->render('avatar',['model'=>$model]);
	}

	//修改头像
	public function actionSavethumb(){
		$user_id = Yii::$app->request->post('user_id');
		$user_thumb =  Yii::$app->request->post('user_thumb');
		$user = User::find()->where(['user_id'=>$user_id])->one();
		$user->user_thumb = $user_thumb;
		if($user->save()){
			echo 1;
		}else{
			echo 2;
		}
	}

	//我的课程
	public function actionCourse(){
		$status = Yii::$app->request->get('status');
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		$pagination = new Pagination([
            'defaultPageSize' => 9,
            'totalCount' => usercourse::find()->where(['and',['user_id'=>$uid,'isLearned'=>0]])->count(),
        ]);
		//(学习中)
		$usercourse = new UserCourse();
		$re = $usercourse->find()->where(['and',['user_id'=>$uid,'isLearned'=>0]])->asarray()->all();
		if(!empty($re)){
		for($i=0;$i<count($re);$i++){
			$data[] = $re[$i]['course_id'];
		}
		
		if(empty($data)){
			$cond = '1=1';
		}else{
			$cond = ['in','course_id',$data];
		}
		$course  = Course::find()
			//->select(['course_id','course_thumb'])
			->offset($pagination->offset)
            ->limit($pagination->limit)
			->where($cond)
			->asarray()
			->all();
		}else{
			$course = '';
		}
        return $this->render('course', [
			'status' => $status,
            'course' => $course,
            'pagination' => $pagination,
        ]);
	}
	//我的课程(完结)
	public function actionCourseend(){
		$status = Yii::$app->request->get('status');
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		$pagination = new Pagination([
            'defaultPageSize' => 9,
            'totalCount' => usercourse::find()->where(['and',['user_id'=>$uid,'isLearned'=>1]])->count(),
        ]);
		$usercourse = new UserCourse();
		$re = $usercourse->find()->where(['and',['user_id'=>$uid,'isLearned'=>1]])->asarray()->all();
		if(!empty($re)){
		for($i=0;$i<count($re);$i++){
			$data[] = $re[$i]['course_id'];
		}
		
		$cond = ['in','course_id',$data];
		$course  = Course::find()
			//->select(['course_id','course_thumb'])
			->offset($pagination->offset)
            ->limit($pagination->limit)
			->where($cond)
			->asarray()
			->all();
		//print_r($course);die;
		}else{
			$course = '';
		}
        return $this->render('course', [
			'status' => $status,
            'course' => $course,
            'pagination' => $pagination,
        ]);
		
	}

	//我收藏的课程
	
	public function actionCoursecollect(){
		$status = Yii::$app->request->get('status');
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		$pagination = new Pagination([
            'defaultPageSize' => 9,
            'totalCount' => Collection::find()->where(['and',['user_id'=>$uid,'collect_type'=>2]])->count(),
        ]);
		$collection = new Collection();
		$re = $collection->find()->where(['and',['user_id'=>$uid,'collect_type'=>2]])->asarray()->all();
		if(!empty($re)){
		for($i=0;$i<count($re);$i++){
			$data[] = $re[$i]['model_id'];
		}
		
		$cond = ['in','course_id',$data];
		$course  = Course::find()
			//->select(['course_id','course_thumb'])
			->offset($pagination->offset)
            ->limit($pagination->limit)
			->where($cond)
			->asarray()
			->all();
		//print_r($course);die;
		}else{
			$course ='';
		}
        return $this->render('course', [
			'status' => $status,
            'course' => $course,
            'pagination' => $pagination,
        ]);
		
	}

	//保存个人信息
	public function actionSavecenter(){
        
        
        $user = new User;
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
        $user->load(Yii::$app->request->post());

        //获取原昵称
        $user_old = $user->find()->select('user_name')
            ->where(['user_id' => $uid])->one();
        
        //ajax验证昵称唯一
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            if ($user->user_name != $user_old->user_name) {
                return \yii\bootstrap\ActiveForm::validate($user);
            } else {
                return ['user-user_name' => []];
            }
        }
        

        //保存数据
		$re = Yii::$app->request->post();
		$data = $re['User'];
		$data['province_id'] = $re['province_id'];
		$data['city_id'] = $re['city_id'];
		$data['area_id'] = $re['area_id'];
        $data['user_opentype'] = '';
			
		$user = $user->find()->where(['user_id'=>$re['User']['user_id']])->one();
		$user->attributes = $data;	
        $user->validate();

        if ($user->validate() && $user->save()) {
				if(isset($re['User']['hobby_id'])){
					$hobby = $re['User']['hobby_id'];
				}else{
					$hobby = '';
				}
            Hobbyrole::deleteAll(['user_id'=>$uid]);		
            if(!empty($hobby)){

                for($i=0;$i<count($hobby);$i++){
                    $hobbyrole = new HobbyRole();
                    $hobbyrole->hobby_id = $hobby[$i];
                    $hobbyrole->user_id = $re['User']['user_id'];
                    $hobbyrole->save();
                }

            }
            echo "<script>alert('保存成功');location.href='".\Yii::$app->urlManager->createUrl('center/index')."'</script>";
        }
	}
	/*
	*	好友列表
	*/
	public function actionMyfriend()
	{
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		$friend = new Friend();
		//好友个数
		$friend_num = $friend->find()->where(['user_id'=>$uid])->count();
		if($friend_num == 0){
			return $this->render('friendlist',['friend_num'=>$friend_num]);
		}
		$friend_list = $friend->find()->where(['user_id'=>$uid])->asarray()->all();
		for($i=0;$i<$friend_num;$i++){
			$data[] = $friend_list[$i]['to_id'];
		}
		if(empty($data)){
			$cond = '1=1';
		}else{
			$cond = ['in', 'user_id', $data];
		}
		$friends = User::find()->where($cond)
						  ->asarray()
						  ->all();
		return $this->render('friendlist',['friends'=>$friends,'friend_num'=>$friend_num]);
	}
	/*
	*	添加好友
	*/
	
	public function actionAddfriend()
	{
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		$usernum = $session->get('user_num');
		$user_num = Yii::$app->request->post('to_id');
		$re = User::find()->where(['user_num'=>$user_num])->asarray()->one();
		$to_id = $re['user_id'];
		$friend = new Friend();
		$friend->user_id = $uid;
		$friend->to_id = $to_id;
		$friend->friend_addtime = time();
		$em = new Easemob();
		$em->addFriend($usernum,$user_num);
		$data['user_id']  = $uid;
		$data['to_id']  = $to_id;
		$data['addtime'] = time();
		$data['message_type'] = 2;
		$data['message_content'] = '添加好友';
		$messages = new UserMessage();
		$messages->attributes = $data;
		if($friend->save() && $messages->save()){
			echo 1;
		}
	}

	/*
	*	删除好友
	*/
	public function actionDelfriend()
	{
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		$user_num = Yii::$app->request->post('user_num');
		$re = User::find()->where(['user_num'=>$user_num])->asarray()->one();
		$friend = new Friend();
		if($friend->deleteAll(['user_id'=>$uid,'to_id'=>$re['user_id']]) && $friend->deleteAll(['user_id'=>$re['user_id'],'to_id'=>$uid])){
			echo 1;
		}
		
	}


	/*
	*	聊天界面
	*/
	
	public function actionChat()
	{
		$session = Yii::$app->session;
		$data['user_num'] = $session->get('user_num');
		$data['user_thumb'] = $session->get('user_thumb');
		$data['user_emid'] = $session->get('user_emid');
		return $this->renderPartial('chat',['data'=>$data]);
	}

	/*
	*	我的钱包
	*/
	public function actionWallet()
	{
		$request = \Yii::$app->request;
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		if(!empty($request->post('virtual_addtime'))){
			if(!empty($request->get('page'))){
			$session->open();
			$virtual_addtime=$session->get('virtual_addtime');
			}else{
				$virtual_addtime = $request->post('virtual_addtime');
				$session->open();
				$session->set('virtual_addtime', $virtual_addtime);
			}
			$pagination = new Pagination([
				'defaultPageSize' => 3,
				'totalCount' => Wallet::find()->where(['and',['user_id'=>$uid],['virtual_addtime'=>time($virtual_addtime)]])->count(),
			]);
			$wallet =Wallet::find()->where(['and',['user_id'=>$uid],['virtual_addtime'=>time($virtual_addtime)]])
			->offset($pagination->offset)
            ->limit($pagination->limit)
			->asarray()
			->all();
			return $this->render('wallet',['wallet'=>$wallet,'virtual_addtime'=>$virtual_addtime,'pagination' => $pagination]);
		}else{
		$pagination = new Pagination([
			'defaultPageSize' => 3,
			'totalCount' => Wallet::find()->where(['user_id'=>$uid])->count(),
		]);
		$wallet =Wallet::find()->where(['user_id'=>$uid])
			->offset($pagination->offset)
            ->limit($pagination->limit)
			->asarray()
			->all();
		return $this->render('wallet',['wallet'=>$wallet,'pagination' => $pagination]);
		}
		
	}
	/*
	*	系统消息
	*/
	public function actionSystem()
	{
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		$pagination = new Pagination([
			'defaultPageSize' => 3,
			'totalCount' => UserMessage::find()->where(['and',['to_id'=>$uid],['message_type'=>2]])->count(),
		]);
		$system = UserMessage::find()
					->select(['message_id','user_id','to_id','message_type','message_type','message_status','addtime','message_operation','message_content'])
					->where(['and',['to_id'=>$uid],['message_type'=>2]])
					->orderby('addtime desc')
					->offset($pagination->offset)
					->limit($pagination->limit)
					->all();
		foreach($system as $k=>$v){
			$system[$k]->message_status = 1;
			$system[$k]->save();
			if($system[$k]->message_content == '添加好友'){
				$system[$k]['message_content'] = User::find()->select(['user_id','user_name','user_thumb'])->where(['user_id'=>$system[$k]->user_id])->asarray()->one();
				$system[$k]['message_type']=1;//添加好友
				$system[$k]['addtime'] = date('Y-m-d H:i:s',$system[$k]['addtime']);
			}else{
				$system[$k]['addtime'] = date('Y-m-d H:i:s',$system[$k]['addtime']);
				$system[$k]['message_type']=0;//系统消息
			}
			
		}		
		//print_r($system);die;
		return $this->render('system',['system'=>$system,'pagination' => $pagination]);
	}

	/*
	*	系统消息操作
	*/
	public function actionOperation()
	{
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		$user = User::find()->where(['user_id'=>$uid])->asarray()->one();
		$message_id = Yii::$app->request->post('message_id');
		$message_operation = Yii::$app->request->post('message_operation');
		$message = UserMessage::find()
					->where(['message_id'=>$message_id])
					->one();
		$message->message_operation = $message_operation;
		if($message_operation == 1){
			$data['message_content']=$user['user_name'].'同意添加你为好友';
			$friend = new Friend();
			$friend->user_id = $message->to_id;
			$friend->to_id = $message->user_id;
			$friend->friend_addtime = time();
			$friend->save();
			$em = new Easemob();
			$em->addFriend('11'.$message->to_id,'11'.$message->user_id);
		}else{
			$data['message_content']=$user['user_name'].'拒绝添加你为好友';
		}		
		if($message->save()){
			$data['user_id']  = $message->to_id;
			$data['to_id']  = $message->user_id;
			$data['addtime'] = time();
			$data['message_type'] = 2;
			$messages = new UserMessage();
			$messages->attributes = $data;
			if($messages->save()){
				echo 1;
			}
		}
	}

	/*
	*	我的回答(问题)
	*/
	public function actionQuestion()
	{
		$status = 'question';
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		$user = User::find()->select(['user_name','user_thumb'])->where(['user_id'=>$uid])->asarray()->one();
		//回答
		$pagination = new Pagination([
			'defaultPageSize' => 3,
			'totalCount' => Answer::find()->where(['user_id'=>$uid])->count(),
		]);
		//问题
		$paginations = new Pagination([
			'defaultPageSize' => 3,
			'totalCount' => Question::find()->where(['user_id'=>$uid])->count(),
		]);
		//收藏
		$paginationss = new Pagination([
			'defaultPageSize' => 3,
			'totalCount' => Collection::find()->where(['and',['user_id'=>$uid],['collect_type'=>3]])->count(),
		]);
		//查询我的所有问题
		$question = Question::find()
					->offset($paginations->offset)
					->limit($paginations->limit)
					->where(['user_id'=>$uid])
					->asarray()
					->all();
		if($question!=''){
		for($i=0;$i<count($question);$i++){
			$result[$i] = Answer::find()->where(['ques_id'=>$question[$i]['ques_id']])->orderby('answer_best,answer_addtime desc')->asarray()->one();
			if(empty($result[$i])){
				$result[$i]['answer_content'] = '当前问题还没有人回答';
			}
			$result[$i]['ques_id'] = $question[$i]['ques_id'];
			$result[$i]['ques_viewnum'] = $question[$i]['ques_viewsum'];
			$result[$i]['ques_answersum'] = $question[$i]['ques_answersum'];
			$result[$i]['ques_addtime'] = $question[$i]['ques_addtime'];
			$result[$i]['ques_name'] = $question[$i]['ques_name'];
			$result[$i]['user_name'] = $user['user_name'];
			$result[$i]['user_thumb'] = $user['user_thumb'];
			if($question[$i]['course_id'] == ''){
					$result[$i]['laiyuan']='有问必答';
				}else{
					$course = Course::find()->where(['course_id'=>$question[$i]['course_id']])->asarray()->one();
					$result[$i]['laiyuan'] = $course['course_name'];
				}
				$label = Queslabel::find()->where(['ques_id'=>$question[$i]['ques_id']])->asarray()->all();
				for($j=0;$j<count($label);$j++){
					$labels[$j] = Questionlabel::find()->where(['queslabel_id'=>$label[$j]['queslabel_id']])->asarray()->one();
					$result[$i]['label'][$j] = $labels[$j]['queslabel_name'];
				}
		}
		}else{
			$result = '';
		}
		return $this->render('question',['result'=>$result,'pagination'=>$pagination,'paginations'=>$paginations,'paginationss'=>$paginationss]);
	}
	
	/*
	*	我的回答(回答)
	*/
	public function actionAnswer()
	{
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		//回答
		$pagination = new Pagination([
			'defaultPageSize' => 3,
			'totalCount' => Answer::find()->where(['user_id'=>$uid])->count(),
		]);
		//问题
		$paginations = new Pagination([
			'defaultPageSize' => 3,
			'totalCount' => Question::find()->where(['user_id'=>$uid])->count(),
		]);
		//收藏
		$paginationss = new Pagination([
			'defaultPageSize' => 3,
			'totalCount' => Collection::find()->where(['and',['user_id'=>$uid],['collect_type'=>3]])->count(),
		]);
		//查询我的回答
		$answer = Answer::find()
					->offset($pagination->offset)
					->limit($pagination->limit)
					->where(['user_id'=>$uid])
					->asarray()
					->all();
		if(!empty($answer)){
			for($i=0;$i<count($answer);$i++){
				//$data[] = $answer[$i]['ques_id'];
				$result[$i]['answer_addtime'] = $answer[$i]['answer_addtime'];
				$result[$i]['answer_content'] = $answer[$i]['answer_content'];
				$question[$i] = Question::find()->where(['ques_id'=>$answer[$i]['ques_id']])->asarray()->one();
			}
			//相关问题
			//$question = Question::find()->where(['in','ques_id',$data])->asarray()->all();
			for($i=0;$i<count($question);$i++){
				//相关用户
				$user[$i] = User::find()->select(['user_name','user_thumb'])->where(['user_id'=>$question[$i]['user_id']])->asarray()->one();
				$result[$i]['ques_id'] = $question[$i]['ques_id'];
				$result[$i]['ques_name'] = $question[$i]['ques_name'];
				$result[$i]['ques_viewsum'] = $question[$i]['ques_viewsum'];
				$result[$i]['ques_answersum'] = $question[$i]['ques_answersum'];
				if($question[$i]['course_id'] == ''){
					$result[$i]['laiyuan']='有问必答';
				}else{
					$course = Course::find()->where(['course_id'=>$question[$i]['course_id']])->asarray()->one();
					$result[$i]['laiyuan'] = $course['course_name'];
				}
				$label = Queslabel::find()->where(['ques_id'=>$question[$i]['ques_id']])->asarray()->all();
				for($j=0;$j<count($label);$j++){
					$labels[$j] = Questionlabel::find()->where(['queslabel_id'=>$label[$j]['queslabel_id']])->asarray()->one();
					$result[$i]['label'][$j] = $labels[$j]['queslabel_name'];
				}
			}
			for($i=0;$i<count($question);$i++){
				
				$result[$i]['user_name'] = $user[$i]['user_name'];
				$result[$i]['user_thumb'] = $user[$i]['user_thumb'];
			}
		}else{
			$result = "";
		}
		return $this->render('answer',['result'=>$result,'pagination'=>$pagination,'paginations'=>$paginations,'paginationss'=>$paginationss]);
	}

	/*
	*	我的回答(收藏)
	*/
	public function actionAnswercollect()
	{
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		//回答
		$pagination = new Pagination([
			'defaultPageSize' => 3,
			'totalCount' => Answer::find()->where(['user_id'=>$uid])->count(),
		]);
		//问题
		$paginations = new Pagination([
			'defaultPageSize' => 3,
			'totalCount' => Question::find()->where(['user_id'=>$uid])->count(),
		]);
		//收藏
		$paginationss = new Pagination([
			'defaultPageSize' => 3,
			'totalCount' => Collection::find()->where(['and',['user_id'=>$uid],['collect_type'=>3]])->count(),
		]);
		//查询我收藏的问答
		$collection = Collection::find()
					->offset($paginationss->offset)
					->limit($paginationss->limit)
					->where(['and',['user_id'=>$uid],['collect_type'=>3]])
					->asarray()
					->all();
		if(!empty($collection)){
			for($i=0;$i<count($collection);$i++){
				$data[] = $collection[$i]['model_id'];
			}
			$question  = Question::find()
						->where(['in','ques_id',$data])
						->asarray()
						->all();
			for($i=0;$i<count($question);$i++){
			//相关用户
			$user[$i] = User::find()->select(['user_name','user_thumb'])->where(['user_id'=>$question[$i]['user_id']])->asarray()->one();
			$result[$i] = Answer::find()
				->where(['ques_id'=>$question[$i]['ques_id']])
				->orderby('answer_best,answer_addtime desc')
				->asarray()
				->one();
			if(empty($result[$i])){
				$result[$i]['answer_content'] = '当前问题还没有人回答';
			}
			$result[$i]['ques_viewnum'] = $question[$i]['ques_viewsum'];
			$result[$i]['ques_answersum'] = $question[$i]['ques_answersum'];
			$result[$i]['ques_addtime'] = $question[$i]['ques_addtime'];
			$result[$i]['ques_name'] = $question[$i]['ques_name'];
			$result[$i]['ques_id'] = $question[$i]['ques_id'];
			if($question[$i]['course_id'] == ''){
					$result[$i]['laiyuan']='有问必答';
				}else{
					$course = Course::find()->where(['course_id'=>$question[$i]['course_id']])->asarray()->one();
					$result[$i]['laiyuan'] = $course['course_name'];
				}
				$label = Queslabel::find()->where(['ques_id'=>$question[$i]['ques_id']])->asarray()->all();
				for($j=0;$j<count($label);$j++){
					$labels[$j] = Questionlabel::find()->where(['queslabel_id'=>$label[$j]['queslabel_id']])->asarray()->one();
					$result[$i]['label'][$j] = $labels[$j]['queslabel_name'];
				}
			}
			for($i=0;$i<count($question);$i++){
				
				$result[$i]['user_name'] = $user[$i]['user_name'];
				$result[$i]['user_thumb'] = $user[$i]['user_thumb'];
			}
		}else{
			$result = '';
		}
		
		return $this->render('answercllect',['result'=>$result,'pagination'=>$pagination,'paginations'=>$paginations,'paginationss'=>$paginationss]);
	}

	/*
	*	我的评论
	*/
	public function actionComment()
	{
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		$uthumb = $session->get('user_thumb');
		$user = User::find()->where(['user_id'=>$uid])->asarray()->one();
		$user_name = $user['user_name'];
		$pagination = new Pagination([
				'defaultPageSize' => 3,
				'totalCount' => Comment::find()->where(['and',['user_id'=>$uid],['comment_status'=>1]])->count(),
			]);
		$comment = Comment::find()
			->select(['comment_id','comment_type','model_id','user_id','user_name','comment_addtime','comment_content'])
			->where(['and',['comment_status'=>1],['user_id'=>$uid]])
			->offset($pagination->offset)
            ->limit($pagination->limit)
			->asarray()
			->all();
		if(!empty($comment)){
		foreach($comment as $k=>$v){
			$comment[$k]['user_thumb'] = $uthumb;
			$comment[$k]['user_name'] = $user_name;
			if(mb_strlen($comment[$k]['comment_content'])>40){
				$comment[$k]['comment_content'] = mb_substr($comment[$k]['comment_content'],0,40,'utf-8').'...';
			}
			if($v['comment_type']==0){
				$comment[$k]['laiyuan'] = Article::find()->select(['article_title'])->where(['article_id'=>$v['model_id']])->asarray()->one();
				if(mb_strlen($comment[$k]['laiyuan']['article_title'])>5){
					$comment[$k]['laiyuan'] = mb_substr($comment[$k]['laiyuan']['article_title'],0,5,'utf-8').'...';
				}else{
					$comment[$k]['laiyuan'] = $comment[$k]['laiyuan']['article_title'];
				}
			}
			if($v['comment_type']==1){
				$comment[$k]['laiyuan'] = Course::find()->select(['course_name'])->where(['course_id'=>$v['model_id']])->asarray()->one();
				if(mb_strlen($comment[$k]['laiyuan']['course_name'])>5){
					$comment[$k]['laiyuan'] = mb_substr($comment[$k]['laiyuan']['course_name'],0,5,'utf-8').'...';
				}else{
					$comment[$k]['laiyuan'] = $comment[$k]['laiyuan']['course_name'];
				}
			}
		}
		}else{
			$comment = '';
		}
		return $this->render('comment',['comment'=>$comment,'pagination' => $pagination]);
	}
	/*
	*	文章收藏
	*/
	public function actionMessage()
	{
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		$collection = new Collection();
		$article = new Article();
		$re = $collection->find()
						->orderby('collect_addtime desc')
						->where(['and',['user_id'=>$uid],['collect_type'=>1]])
						->asarray()
						->all();
		$pagination = new Pagination([
            'defaultPageSize' => 3,
            'totalCount' => $collection->find()->where(['and',['user_id'=>$uid],['collect_type'=>1]])->count(),
        ]);
		if(!empty($re)){
		for($i=0;$i<count($re);$i++){
			$data[] = $re[$i]['model_id'];
		}
		
		$cond = ['in','article_id',$data];
		$articles  = $article->find()
			->select(['article_id','article_title','article_descripte','article_addtime','article_thumb','article_count','article_author'])
			->offset($pagination->offset)
            ->limit($pagination->limit)
			->where($cond)
			->asarray()
			->all();
		}else{
			$articles = '';
		}
        return $this->render('message', [
            'articles' => $articles,
            'pagination' => $pagination,
        ]);
	}
	
	
	
	/*
	*	删除文章收藏
	*/
	public function actionDelarticle()
	{
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		$collection = new Collection();
		$article_id =  Yii::$app->request->post();
		if(!is_int($article_id)){
			$article_id = explode(",", $article_id['article_id']); 
		}
		//print_r($article_id);die;
		$re =$collection->deleteAll(['and','user_id'=>$uid,'collect_type'=>1,['in','model_id',$article_id]]);
		if($re){
			echo 1;
		}
		
	
	}

	/*
	*	喜欢教师
	*/
	public function actionLiketeacher()
	{
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		$pagination = new Pagination([
			'defaultPageSize' => 6,
			'totalCount' => Like::find()->where(['user_id'=>$uid])->count(),
		]);
		$re = Like::find()->where(['user_id'=>$uid])->asarray()->all();
		if(!empty($re)){
		for($i=0;$i<count($re);$i++){
			$data[] = $re[$i]['teacher_id'];
		}
		$model = new Teacher();
		$teacher = $model->TheacherInfo($data,$pagination);
		}else{
			$teacher = '';
		}
		return $this->render('liketeacher',['teacher'=>$teacher,'pagination'=>$pagination]);
	}
	/*
	*	取消喜欢教师
	*/
	public function actionDelteacher()
	{
		$session = Yii::$app->session;
		$uid = $session->get('user_id');
		$teacher_id =  Yii::$app->request->post('teacher_id');
		$re = Like::deleteAll(['and',['teacher_id'=>$teacher_id],['user_id'=>$uid]]);
		if($re){
			echo 1;
		}
	}
	/*
	* 获取手机的验证码
	*/
	public function actionCodes()
	{
		if(Yii::app()->request->hostInfo != 'http://www.bwphp.com'){
			echo '非法请求';die;
		}else{
			$phone=Yii::$app->request->post('phone');
			$code = new Code();
			echo $code->getCode($phone);
		}
		
	}
	/*
	* 验证手机的验证码
	*/
	public function actionValidationcodes()
	{
		$phone=Yii::$app->request->post('phone');
		$codes=Yii::$app->request->post('code');
		$code = new Code();
		$re = $code->getUsercode($phone);
		if($re == ''){
			echo 1;
		}else if($re == $codes){
			echo 2;
		}else{
			echo 3;
		}
	}
	/*
	* 修改手机
	*/
	public function actionSavephone()
	{
		$phone=Yii::$app->request->post('phone');
		$new_phone=Yii::$app->request->post('new_phone');
		$user =User::find()->where(['user_phone'=>$phone])->one();
		$user->user_phone = $new_phone;
		if($user->save()){
			echo 1;
		}else{
			echo 2;
		}
	}
	/*
	*	绑定手机
	*/
	public function actionBdphone()
	{
		$phone=Yii::$app->request->post('phone');
		$user_id=Yii::$app->request->post('user_id');
		$user =User::find()->where(['user_id'=>$user_id])->one();
		$user->user_phone = $phone;
		if($user->save()){
			echo 1;
		}else{
			echo 2;
		}
	}
	/*
	* 修改邮箱
	*/
	public function actionSaveemail()
	{
		$email=Yii::$app->request->post('email');
		$new_email=Yii::$app->request->post('new_email');
		$user =User::find()->where(['user_email'=>$email])->one();
		$user->user_email = $new_email;
		if($user->save()){
			echo 1;
		}else{
			echo 2;
		}
	}
	/*
	*	绑定手机
	*/
	public function actionBdemail()
	{
		$email=Yii::$app->request->post('email');
		$user_id=Yii::$app->request->post('user_id');
		$user =User::find()->where(['user_id'=>$user_id])->one();
		$user->user_email = $email;
		if($user->save()){
			echo 1;
		}else{
			echo 2;
		}
	}
	public function actionSel()
	{
		$id=Yii::$app->request->post('id');
		$region = new Region();
		$arr = $region->find()->where(['parent_id'=>$id])->asarray()->all();
		echo json_encode($arr);
	}
	/*
	*	修改密码
	*/
	public function actionSavepwd()
	{
		$user_pwd=md5(Yii::$app->request->post('user_pwd'));
		$email_phone = Yii::$app->request->post('email_phone');
		$user = User::find()->where(['or',['user_phone'=>$email_phone],['user_email'=>$email_phone]])->one();
		$user->user_pwd = $user_pwd;
		if($user->save()){
			echo 1;
		}
	}
	/*
	*	新手机
	*/
	public function actionNewphone()
	{
		$codes = new Codes();
		$request = Yii::$app->request;
		$codes->load($_POST);
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return \yii\bootstrap\ActiveForm::validate($codes);
        }
		$data = $request->post('Codes');
		print_r($data);die;
		$code = new Code();
		echo $code->getCode($data['user_email_phone']);
	}
	/*
	*	新邮箱
	*/
	public function actionNewemail()
	{
		$codee = new Codee();
		$request = Yii::$app->request;
		$codee->load($_POST);
		if (Yii::$app->request->isAjax) {
           Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
		   return \yii\bootstrap\ActiveForm::validate($codee);
        }
		echo 123;die;
		$data = $request->post('Codes');
		$code = new Code();
		echo $code->getCode($data['user_email_phone']);
	}
}
?>